package org.example.Agricultural_33.service;

import java.lang.Exception;
import java.lang.String;
import java.util.Arrays;
import javax.annotation.PostConstruct;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityAddInspectionInputBO;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityCreateProductInputBO;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityDeleteProductInputBO;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityGetProductInputBO;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityPurchaseProductInputBO;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityRestoreProductInputBO;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityTransferOwnershipInputBO;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityUpdateProductInfoInputBO;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityVerifyDataIntegrityInputBO;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
import org.fisco.bcos.sdk.transaction.model.dto.CallResponse;
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.example.Agricultural_33.model.bo.AgriculturalTraceabilityGetOwnershipTransferInputBO;

@Service
@NoArgsConstructor
@Data
public class AgriculturalTraceabilityService {
  public static final String ABI = org.example.Agricultural_33.utils.IOUtil.readResourceAsString("abi/AgriculturalTraceability.abi");

  public static final String BINARY = org.example.Agricultural_33.utils.IOUtil.readResourceAsString("bin/ecc/AgriculturalTraceability.bin");

  public static final String SM_BINARY = org.example.Agricultural_33.utils.IOUtil.readResourceAsString("bin/sm/AgriculturalTraceability.bin");

  @Value("${system.contract.agriculturalTraceabilityAddress}")
  private String address;

  @Autowired
  private Client client;

  AssembleTransactionProcessor txProcessor;

  @PostConstruct
  public void init() throws Exception {
    this.txProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor(this.client, this.client.getCryptoSuite().getCryptoKeyPair());
  }

  public CallResponse owner() throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "owner", Arrays.asList());
  }

  public TransactionResponse updateProductInfo(AgriculturalTraceabilityUpdateProductInfoInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "updateProductInfo", input.toArgs());
  }

  public TransactionResponse deleteProduct(AgriculturalTraceabilityDeleteProductInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "deleteProduct", input.toArgs());
  }

  public TransactionResponse purchaseProduct(AgriculturalTraceabilityPurchaseProductInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "purchaseProduct", input.toArgs());
  }

  public CallResponse getProduct(AgriculturalTraceabilityGetProductInputBO input) throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "getProduct", input.toArgs());
  }

  public CallResponse getContractInfo() throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "getContractInfo", Arrays.asList());
  }

  public TransactionResponse createProduct(AgriculturalTraceabilityCreateProductInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "createProduct", input.toArgs());
  }

  public TransactionResponse restoreProduct(AgriculturalTraceabilityRestoreProductInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "restoreProduct", input.toArgs());
  }

  public CallResponse getAllProductIds() throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "getAllProductIds", Arrays.asList());
  }

  public CallResponse verifyDataIntegrity(AgriculturalTraceabilityVerifyDataIntegrityInputBO input) throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "verifyDataIntegrity", input.toArgs());
  }

  public TransactionResponse addInspection(AgriculturalTraceabilityAddInspectionInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "addInspection", input.toArgs());
  }

  public TransactionResponse transferOwnership(AgriculturalTraceabilityTransferOwnershipInputBO input) throws Exception {
    return this.txProcessor.sendTransactionAndGetResponse(this.address, ABI, "transferOwnership", input.toArgs());
  }

  public CallResponse getOwnershipTransfer(AgriculturalTraceabilityGetOwnershipTransferInputBO input) throws Exception {
    return this.txProcessor.sendCall(this.client.getCryptoSuite().getCryptoKeyPair().getAddress(), this.address, ABI, "getOwnershipTransfer", input.toArgs());
  }
}
